package cn.ht.jywrwater.module.ht.service.watercount;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.ht.jywrwater.module.ht.controller.admin.watercount.vo.*;
import cn.ht.jywrwater.module.ht.dal.dataobject.watercount.WaterCountDO;
import cn.ht.jywrwater.framework.common.pojo.PageResult;

import cn.ht.jywrwater.module.ht.convert.watercount.WaterCountConvert;
import cn.ht.jywrwater.module.ht.dal.mysql.watercount.WaterCountMapper;

import static cn.ht.jywrwater.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ht.jywrwater.module.ht.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 水量统计 Service 实现类
 *
 * @author hzx
 */
@Service
@Validated
public class WaterCountServiceImpl implements WaterCountService {

    @Resource
    private WaterCountMapper waterCountMapper;

    @Override
    public Long createWaterCount(WaterCountCreateReqVO createReqVO) {
        // 插入
        WaterCountDO waterCount = WaterCountConvert.INSTANCE.convert(createReqVO);
        waterCountMapper.insert(waterCount);
        // 返回
        return waterCount.getId();
    }

    @Override
    public void updateWaterCount(WaterCountUpdateReqVO updateReqVO) {
        // 校验存在
        validateWaterCountExists(updateReqVO.getId());
        // 更新
        WaterCountDO updateObj = WaterCountConvert.INSTANCE.convert(updateReqVO);
        waterCountMapper.updateById(updateObj);
    }

    @Override
    public void deleteWaterCount(Long id) {
        // 校验存在
        validateWaterCountExists(id);
        // 删除
        waterCountMapper.deleteById(id);
    }

    private void validateWaterCountExists(Long id) {
        if (waterCountMapper.selectById(id) == null) {
            throw exception(WATER_COUNT_NOT_EXISTS);
        }
    }

    @Override
    public WaterCountDO getWaterCount(Long id) {
        return waterCountMapper.selectById(id);
    }

    @Override
    public List<WaterCountDO> getWaterCountList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return waterCountMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<WaterCountDO> getWaterCountPage(WaterCountPageReqVO pageReqVO) {
        return waterCountMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WaterCountDO> getWaterCountList(WaterCountExportReqVO exportReqVO) {
        return waterCountMapper.selectList(exportReqVO);
    }

}
